package config

import (
	"github.com/spf13/viper"
	"github.com/toolkits/pkg/errorx"
	"sync"
	"web-service-gin/src/pkg/logx"

	"web-service-gin/src/pkg/httpx"
	"web-service-gin/src/pkg/storage"
)

var (
	C    = new(Config)
	once sync.Once
)

type Config struct {
	RunMode string
	PProf   bool
	Redis   storage.RedisConf
	DB      storage.DBConfig
	Http    httpx.HttpConfig
	Log     logx.LogConfig
}

func MustLoad(path string) {
	once.Do(
		func() {
			viper.SetConfigFile(path)

			if err := viper.ReadInConfig(); err != nil {
				errorx.Bomb(1, "fatal error config file: %v", err)
			}

			if err := viper.Unmarshal(C); err != nil {
				errorx.Bomb(1, "unable to decode into struct, %v", err)
			}

		})
}
